package com.dd.web.base.entity.common;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
@ApiOperation("page对象")
public class CommonPage<K> {
    @ApiModelProperty("当前页数")
    private Integer pageNum;
    @ApiModelProperty("每页行数")
    private Integer pageSize;
    @ApiModelProperty("总行数")
    private Integer totalPage;
    @ApiModelProperty("总行数")
    private Long total;
    @ApiModelProperty("数据列表")
    private List<K> list;
//    @ApiModelProperty("查询条件对象")
//    private V queryParams;

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <K> CommonPage<K> restPage(List<K> list) {
        CommonPage<K> result = new CommonPage<K>();
        PageInfo<K> pageInfo = new PageInfo<K>(list);
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
    public static <K> CommonPage<K> restPage(Page<K> pageInfo) {
        CommonPage<K> result = new CommonPage<K>();
        result.setTotalPage(pageInfo.getTotalPages());
        result.setPageNum(pageInfo.getNumber());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotalElements());
        result.setList(pageInfo.getContent());
        return result;
    }

}
